﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class DisplayWeek: System.Web.UI.Page
{
    List<Game> games;
    List<UserPick> userPicks;
    User user;
    List<GameControl> gcs = new List<GameControl>();
    protected void Page_Load(object sender, EventArgs e)
    {

        Team team = null;

        var seasonQuery = Request.QueryString["Season"];
        Season s = null;

        if (seasonQuery != null)
            s = DataBase.Seasons.GetSeasonWithID(seasonQuery);
        else
            s = DataBase.Seasons.GetCurrentSeason();


        if (Request.QueryString["Team"] != null)
            team = DataBase.Teams.GetTeamWithID(Request.QueryString["Team"]);
        else
            Response.Redirect("DisplayWeek.aspx");

        seasonsControl.GenerateSeasons(s, "DisplayTeamGames.aspx?Team=" + Request.QueryString["Team"]);

        teamHeader.InnerText = team.TeamName;
        this.games = DataBase.Games.GetGamesForTeam(team, s);
        userPicks = DataBase.UserPicks.GetAllUserPicksForTeam(team);
        if (Session["userID"] != null)
                user = DataBase.Users.GetUserWithID(Session["userID"].ToString());

        foreach (Game game in games)
        {
            var gamePicks = (from u in userPicks
                             where u.GameID == game.ID
                             select u).ToList();

            GameControl gc = (GameControl)Page.LoadControl("~/GameControl.ascx");

            if (user != null)
            {
                gc.Init(game, gamePicks, user);
            }
            else
                gc.Init(game, gamePicks);

            gcs.Add(gc);
            gameControls.Controls.Add(gc);
        }


    }




    protected void Page_LoadComplete(object sender, EventArgs e)
    {

        foreach (GameControl gc in gcs)
        {
            gc.LoadGame();
        }

    }

    protected void MaintainScroll(GameControl gc)
    {
        string script = @" window.onload = function Maintain()
                              {var con = document.getElementById('" + gc.ClientID + "_gameControl" + "');con.scrollIntoView(); }";
        this.ClientScript.RegisterStartupScript(this.GetType(), "Maintain", script, true);
    }


}